package com.wx.controller;

import com.bean.BootStrapTableExpress;
import com.bean.Express;
import com.bean.Message;
import com.bean.User;
import com.mvc.ResponseBody;
import com.mvc.ResponseView;
import com.service.ExpressService;
import com.util.ExpressEnum;
import com.util.JSONUtil;
import com.util.TurnUtil;
import com.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Stream;

/**
 * @Author: 柏竹
 * @Description: 微信 快递业务管理
 * @Date_Created_in: 2021-05-30 8:57
 * @Modified_By: Sans
 * @Project: 快递e栈
 */
public class ExpressController {
    
    @ResponseBody("/wx/findExpressAll.do")
    public String findAll(HttpServletRequest req, HttpServletResponse resp){
        Message msg = null;
        BootStrapTableExpress express1 = (BootStrapTableExpress) req.getSession().getAttribute("Express");
        if (express1 != null){
            msg = new Message(2,express1);
            //删除存储限于一次使用
            req.getSession().removeAttribute("Express");
            return JSONUtil.toJSON(msg);
        }
        
        User user = UserUtil.getWxUser(req.getSession());
        
        if (user == null){
            msg = new Message("账户以丢失",-1,null);
            return JSONUtil.toJSON(msg);
        }
        List<Express> expressList = ExpressService.findExpress(ExpressEnum.USERPHONE.type , user.getUserphone());
        if (expressList == null){
            msg = new Message("暂无快递",1,null);
            return JSONUtil.toJSON(msg);
        }
        if (expressList.size()>0){
            Stream<Express> status0Express = expressList.stream().filter(express -> {
                if (express.getStatus() == 0) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1 , o2) -> {
                long o1Time = o1.getIntime().getTime();
                long o2Time = o2.getIntime().getTime();
                return (int) (o1Time - o2Time);
            });
    
            Stream<Express> status1Express = expressList.stream().filter(express ->{
                if (express.getStatus()==1){
                    return true;
                }else{
                    return false;
                }
            }).sorted((o1 , o2) -> {
                long o1Time = o1.getIntime().getTime();
                long o2Time = o2.getIntime().getTime();
                return (int)(o1Time-o2Time);
            });
            
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            List<BootStrapTableExpress> expressList0 = TurnUtil.setBootStrapTableExpress(new ArrayList(Arrays.asList(s0)));
            List<BootStrapTableExpress> expressList1 = TurnUtil.setBootStrapTableExpress(new ArrayList(Arrays.asList(s1)));
            Map data = new HashMap();
            data.put("status0",expressList0);
            data.put("status1",expressList1);
            msg = new Message(0,data);
        }
        return JSONUtil.toJSON(msg);
    }
    
    
    @ResponseBody("/wx/userExpressByStatusList.do")
    public String userExpressByStatusList(HttpServletRequest req, HttpServletResponse resp){
        String userPhone = req.getParameter("userPhone");
        Message msg = null;
        
        List<Express> expressList = ExpressService.findExpress(ExpressEnum.USERPHONE.type , userPhone);
        if (expressList == null){
            msg = new Message("暂无快递",1,null);
            return JSONUtil.toJSON(msg);
        }
        if (expressList.size()>0){
            Stream<Express> status0Express = expressList.stream().filter(express -> {
                if (express.getStatus() == 0) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1 , o2) -> {
                long o1Time = o1.getIntime().getTime();
                long o2Time = o2.getIntime().getTime();
                return (int) (o1Time - o2Time);
            });
        
            Stream<Express> status1Express = expressList.stream().filter(express ->{
                if (express.getStatus()==1){
                    return true;
                }else{
                    return false;
                }
            }).sorted((o1 , o2) -> {
                long o1Time = o1.getIntime().getTime();
                long o2Time = o2.getIntime().getTime();
                return (int)(o1Time-o2Time);
            });
        
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            List<BootStrapTableExpress> expressList0 = TurnUtil.setBootStrapTableExpress(new ArrayList(Arrays.asList(s0)));
            List<BootStrapTableExpress> expressList1 = TurnUtil.setBootStrapTableExpress(new ArrayList(Arrays.asList(s1)));
            Map data = new HashMap();
            data.put("status0",expressList0);
            data.put("status1",expressList1);
            msg = new Message(0,data);
        }
        return JSONUtil.toJSON(msg);
        
        
        // // String userPhone = "14444444444";
        // List<Express> express = ExpressService.findByUserPhoneAndStatus(userPhone,0);
        // Message msg = null;
        // if (express == null){
        //     msg = new Message("未查到指定快递",-1,null);
        //     return JSONUtil.toJSON(msg);
        // }
        // if (express.size()>0){
        //     List<BootStrapTableExpress> expressList = TurnUtil.setBootStrapTableExpress(express);
        //     msg = new Message("查询成功",0,expressList);
        // }else{
        //     msg = new Message("未查到指定快递",-1,null);
        // }
        // return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        // String code = "222222";
        Message msg = null;
        List<Express> express = ExpressService.findExpress(ExpressEnum.CODE.type , code);
        if (express == null){
            msg = new Message("无此快递",-1,null);
            return JSONUtil.toJSON(msg);
        }
        List<BootStrapTableExpress> expressList = TurnUtil.setBootStrapTableExpress(express);
        msg = new Message("查询成功",0,expressList);
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody("/wx/findExpressByNumber.do")
    public String findExpressByIdNumber(HttpServletRequest req, HttpServletResponse resp){
        String expressNum = req.getParameter("expressNum");
        List<Express> express = ExpressService.findExpress(ExpressEnum.NUMBER.type , expressNum);
        Message msg = null;
        if (express == null){
            msg = new Message("无此快件",-1,null);
        }else{
            String userPhone = UserUtil.getWxUser(req.getSession()).getUserphone();
            String expressPhone = express.get(0).getUserphone();
            if (userPhone.equals(expressPhone)){
                msg = new Message(null,0,null);
                List<BootStrapTableExpress> expressList = TurnUtil.setBootStrapTableExpress(express);
                req.getSession().setAttribute("Express",expressList.get(0));
            }else{
                msg = new Message("无此快件",-1,null);
            }
        }
        return JSONUtil.toJSON(msg);
    }
    
    
}
